#include "base/cpuinfo.h"

#include "base/config.h"
#include <stdio.h>
#include <math.h>

namespace net_tools::base{
int Cpuinfo::cpu_core_number_ = 0;
int Cpuinfo::cpu_loadavg_ = 0;

void Cpuinfo::Loadcpuinfo(){
  if(cpu_core_number_ != 0){
    return;
  }
  FILE* popen_;
  char core_char[_C::CORE_NUMBER_BIT] = {};
  popen_ = popen("grep ^'cpu cores' /proc/cpuinfo|uniq|awk -F: '{ print $2 }'|sed 's/^.//g'","r");
  fread(core_char,1,1,popen_);
  fclose(popen_);
  int bit = 0;
  for(int count = _C::CORE_NUMBER_BIT - 1;count >= 0;--count){
    if(core_char[count] <= '9' && core_char[count] >= '0'){
      cpu_core_number_ += (core_char[count] - '0') * pow(10,bit);
      bit++;
    }
  }
};

} // namespace net_tools::base